package com.bbzn.device.client.controller;

import com.bbzn.device.client.vo.req.IotCardVO;
import com.bbzn.device.client.aspect.OperationAnnotation;
import com.bbzn.device.client.constant.IotCardConstant;
import com.bbzn.device.client.constant.PhoUserConstant;
import com.bbzn.device.client.constant.RedisConstant;
import com.bbzn.device.client.dataobject.IotCard;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.exception.DefinitException;
import com.bbzn.device.client.service.CommonService;
import com.bbzn.device.client.service.IotCardService;
import com.bbzn.device.client.service.SystemParamByCacheService;
import com.bbzn.device.client.utils.BaseApiService;
import com.bbzn.device.client.utils.BaseResponse;
import com.bbzn.device.client.utils.MayiktJwtUtils;
import com.bbzn.iotcard.client.IotcardOrderClient;
import com.bbzn.iotcard.comment.api.client.com.ClientResult;
import com.bbzn.iotcard.comment.api.client.req.IotProductReq;
import com.bbzn.iotcard.comment.api.client.req.IotcardDetailReq;
import com.bbzn.iotcard.comment.api.client.req.Traffic;
import com.bbzn.iotcard.comment.api.client.res.IotProductRes;
import com.bbzn.iotcard.comment.api.client.res.IotcardDetailRes;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/** knife4j
 * @Author: jncehuang
 * @Date : 2021/3/29 17:04
 * @Version 1.0
 */
@Slf4j
@Api(value = "IOT", tags = "IOT服务")
@RestController
@RequestMapping("/iotCard")
//@CrossOrigin
public class IotCardController extends BaseApiService {

    @Autowired
    IotCardService iotCardService;

    @Autowired
    private CommonService commonService;

    @Autowired
    private SystemParamByCacheService systemParamByCacheService;

    @Autowired
    private IotcardOrderClient iotcardOrderClient;


    /**
     * 查询卡信息
     * @param reqVO
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getSimByParamTest", method = RequestMethod.POST)
    @OperationAnnotation(content = "查询卡信息")
    public BaseResponse getSimByParamTest(@RequestBody IotCardVO reqVO , HttpServletRequest request) throws  Exception{
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        if (Objects.isNull(reqVO.getCardNumber())){
            systemParam = systemParamByCacheService.getValues(IotCardConstant.IOTCARD_ENUM, IotCardConstant.IOTCARD_NO_ICCID_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        /*Map msg=iotCardService.getSimByParam(reqVO);*/
        Map<String, Object> msg = new HashMap<>();

        /**
         * 先查询数据库中的数据，点击刷新才是实时
         * 查询卡信息
         */
        //查询用户流量卡信息
        IotCard iotCard = iotCardService.findOneByCardNum(reqVO.getCardNumber());
        if (Objects.isNull(iotCard)){
            systemParam = systemParamByCacheService.getValues(IotCardConstant.IOTCARD_ENUM, IotCardConstant.IOTCARD_NO_DATA_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        IotcardDetailReq iotcardDetailReq = new IotcardDetailReq();
        iotcardDetailReq.setIccid(iotCard.getId().toString());
        ClientResult<List<IotcardDetailRes>> iotcardDetailRes = iotcardOrderClient.getIotcardInfo(iotcardDetailReq);
        if (iotcardDetailRes.getCode() != 0){
            throw new DefinitException(iotcardDetailRes.getCode(),iotcardDetailRes.getMsg());
        }
        msg.put("iotcardDetail",iotcardDetailRes);
        return  setResultSuccess(msg) ;
    }

    /**
     * 刷新流量卡信息
     * @param reqVO
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/refreshIotCard", method = RequestMethod.POST)
    @OperationAnnotation(content = "刷新流量卡信息")
    public BaseResponse refreshIotCard(@RequestBody IotCardVO reqVO , HttpServletRequest request) throws  Exception{
        Map<String, Object> msg = new HashMap<>();
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        if (Objects.isNull(reqVO.getCardNumber())){
            systemParam = systemParamByCacheService.getValues(PhoUserConstant.PHO_USER_ENUM, PhoUserConstant.PHO_UN_PHONE_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        /**
         * 1、更新流量 拿到最新使用数据
         * 2、查询卡信息
         */
        Traffic traffic = new Traffic();
        traffic.setIccid(reqVO.getCardNumber());
        //traffic.setNeedDtl("0");
        ClientResult<Void> tra = iotcardOrderClient.getNowTraffic(traffic);
        if (tra.getCode() != 0){
            throw new DefinitException(tra.getCode(),tra.getMsg());
        }
        //查询用户流量卡信息
        IotCard iotCard = iotCardService.findOneByCardNum(reqVO.getCardNumber());
        IotcardDetailReq iotcardDetailReq = new IotcardDetailReq();
        iotcardDetailReq.setIccid(iotCard.getId().toString());
        ClientResult<List<IotcardDetailRes>> iotcardDetailRes = iotcardOrderClient.getIotcardInfo(iotcardDetailReq);
        if (iotcardDetailRes.getCode() != 0){
            throw new DefinitException(tra.getCode(),tra.getMsg());
        }
        msg.put("iotcardDetail",iotcardDetailRes);
        return  setResultSuccess(msg) ;
    }


    /**
     * 获取基础套餐列表
     * @param reqVO
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getIotProductByBasicList", method = RequestMethod.POST)
    @OperationAnnotation(content = "获取基础套餐列表")
    public BaseResponse getProductByBasicList(@RequestBody IotCardVO reqVO , HttpServletRequest request) throws  Exception{
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        if (Objects.isNull(reqVO.getCardNumber())){
            systemParam = systemParamByCacheService.getValues(PhoUserConstant.PHO_USER_ENUM, PhoUserConstant.PHO_UN_PHONE_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        /*Map msg=iotCardService.getSimByParam(reqVO);*/
        Map<String, Object> msg = new HashMap<>();

        IotProductReq iotProductReq = new IotProductReq();
        iotProductReq.setStartIndex((reqVO.getPageNum() - 1) * reqVO.getPageSize());
        ClientResult<List<IotProductRes>> productResLists = iotcardOrderClient.getProductList(iotProductReq);
        List<IotProductRes> iotProductResList = productResLists.getData();
        List<IotProductRes> basics = iotProductResList.stream().filter(n -> !Objects.isNull(n.getProductType())&& n.getProductType().equals("0")).collect(Collectors.toList());   //基础套餐

        msg.put("basics",basics);
        return  setResultSuccess(msg) ;
    }


    /**
     * 获取叠加包列表
     * @param reqVO
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getProductBySuperpositionList", method = RequestMethod.POST)
    @OperationAnnotation(content = "获取叠加包列表")
    public BaseResponse getProductBySuperpositionList(@RequestBody IotCardVO reqVO , HttpServletRequest request) throws  Exception{
        SystemParam systemParam = null;
        String token = request.getHeader("token");
        String language = commonService.getLanguage(token);
        if(StringUtils.isEmpty(token)){
            systemParam = systemParamByCacheService.getValues(RedisConstant.REDIS_ENUM, RedisConstant.TOKEN_INVALID_PREFIX + language);
            throw new DefinitException(systemParam.getCode(),systemParam.getMassage());
        }
        if (Objects.isNull(reqVO.getCardNumber())){
            systemParam = systemParamByCacheService.getValues(PhoUserConstant.PHO_USER_ENUM, PhoUserConstant.PHO_UN_PHONE_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        /*Map msg=iotCardService.getSimByParam(reqVO);*/
        Map<String, Object> msg = new HashMap<>();

        IotProductReq iotProductReq = new IotProductReq();
        iotProductReq.setStartIndex((reqVO.getPageNum() - 1) * reqVO.getPageSize());
        ClientResult<List<IotProductRes>> productResLists = iotcardOrderClient.getProductList(iotProductReq);
        List<IotProductRes> iotProductResList = productResLists.getData();
        List<IotProductRes> superpositions = iotProductResList.stream().filter(n -> !Objects.isNull(n.getProductType())&& n.getProductType().equals("1")).collect(Collectors.toList());   //叠加包

        msg.put("superpositions",superpositions);
        return  setResultSuccess(msg) ;
    }

}
